﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Windows.Media;
using Common;
using ImageProcessing.Common;
using ImageProcessing.Engine;
using ImageProcessing.Storage;
using WcfService.DataContracts;

namespace WcfService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class wp7ort : Iwp7ort, IDisposable
    {
        private readonly OnlineImageProcessingEngine _onlineImageProcessingEngine = new OnlineImageProcessingEngine();
        private readonly IImageArchiver _imageArchiver = new OnlineImageArchiver();

        public wp7ort()
        {
            _onlineImageProcessingEngine.DBConnection = new OnlinePersistanceDB();
            _onlineImageProcessingEngine.Initialise();
        }

        public void AddImage(ImageData imageData)
        {
            byte[] yUncompressedData = CompressionHelpers.Uncompress(imageData.ZipCompressedYData);
            byte[] argbUncompressedDataBytes = CompressionHelpers.Uncompress(imageData.ZipCompressedArgbData);
            int[] argbUncompressedData = CompressionHelpers.ConvertByteArrayToIntArray(argbUncompressedDataBytes);

            _imageArchiver.SaveRawImage(imageData.TagInfo, imageData.Width, imageData.Height, ref yUncompressedData);
            HelperMethods.SaveImageToJpeg(imageData.TagInfo, PixelFormats.Gray8, imageData.Width, imageData.Height, yUncompressedData);

            // todo fix
            //SaveImage(picId, PixelFormats.Pbgra32, ImageData.Width, ImageData.Height, argbUncompressedDataBytes);

            _onlineImageProcessingEngine.LearnImage(imageData.TagInfo, imageData.Width, imageData.Height,
                ref argbUncompressedData, ref yUncompressedData);
        }



        public LookupPointsResponse LookupPoints(LookupPointsRequest lookupPointsRequest)
        {

            List<InterestPoint> interestPoints = new List<InterestPoint>();
            foreach (PointInfo pointInfo in lookupPointsRequest.PointsInfo)
            {
                InterestPoint ip = new InterestPoint(pointInfo);
                interestPoints.Add(ip);
            }

            PointsResultProcessor prp = _onlineImageProcessingEngine.MatchPoints(interestPoints);

            if (prp.BestFoundObject != null)
                foreach (InterestPoint interestPoint in prp.BestFoundObject.FoundPoints)
                {
                    if (!interestPoint.IsValidDescriptor || interestPoint.TagInfo == null)
                        throw new Exception("Point is not valid");
                }

            return prp.BestFoundObject != null ? new LookupPointsResponse(prp) : new LookupPointsResponse();
        }

        public AnimationOverlay LookupOverlay(TagInfo tagInfo)
        {
            throw new NotImplementedException();
        }

        public void AddOverLay(TagInfo tagInfo, AnimationOverlay animationOverlay)
        {
            throw new NotImplementedException();
        }

        public ImageData GetImage(TagInfo tagInfo)
        {
            string fileName = HelperMethods.GetImageFileNamePath(tagInfo.TagId.ToString() + HelperMethods.RawFileExtension);
            LoadedImage loadedImage = HelperMethods.LoadYImage(fileName);

            ImageData imageData = new ImageData
                                      {
                                          Width = loadedImage.width,
                                          Height = loadedImage.height,
                                          TagInfo = loadedImage.tagInfo,
                                          ZipCompressedYData = CompressionHelpers.Compress(loadedImage.rawImage, tagInfo.TagName)
                                      };

            return imageData;
        }

        public GetImagesResponse GetImages(GetImagesRequest getImagesRequest)
        {
            return new GetImagesResponse() { Images = _imageArchiver.ImageList.ToArray() };
        }

        public void Dispose()
        {
            _onlineImageProcessingEngine.Dispose();
        }
    }
}
